import { get, post } from '../utils/request';
import { ApiResponse, LoginRequest, RegisterRequest, AuthResponse } from '../types';

/**
 * 用户登录
 */
export function login(data: LoginRequest) {
  return post<ApiResponse<AuthResponse>>('/auth/login', data);
}

/**
 * 用户注册
 */
export function register(data: RegisterRequest) {
  return post<ApiResponse<AuthResponse>>('/auth/register', data);
}

/**
 * 邮箱验证
 */
export function verifyEmail(code: string) {
  return get<ApiResponse<null>>(`/auth/verify-email?code=${code}`);
}

/**
 * 忘记密码
 */
export function forgotPassword(email: string) {
  const params = new URLSearchParams();
  params.append('email', email);
  return post<ApiResponse<null>>('/auth/forgot-password', params, {
    timeout: 60000, // 邮件发送操作设置60秒超时
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

/**
 * 重置密码
 */
export function resetPassword(data: { code: string; newPassword: string }) {
  return post<ApiResponse<null>>('/auth/reset-password', data);
}

/**
 * 测试密码匹配（调试用）
 */
export function testPassword(username: string, password: string) {
  return get<ApiResponse<{ match: boolean; details: string }>>(`/auth/test-password?username=${username}&password=${password}`);
} 